import { useStore } from 'vuex'
import { ref , computed , ComputedRef} from 'vue'

interface LoadParams{   //定义加载参数的类型
    currentPage:number;
    pageSize:number;
}
const useLoadMore = (actionName:string,total:ComputedRef<number>,id:string,
    params:LoadParams={currentPage:2,pageSize:5}) =>{
    const store = useStore()    //返回 this.$store
    const currentPage = ref(params.currentPage) //获取传过来的当前页数
    const requestParams = computed(()=>({   //请求参数有当前页和页面数据大小
        currentPage:currentPage.value,
        pageSize:params.pageSize,
    }))
    const loadMorePage = ()=>{
        if(id!=='' as unknown){
            store.dispatch(actionName,{params:requestParams.value,id}).then(()=>{
                currentPage.value++
            })
        }else{
            store.dispatch(actionName,requestParams.value).then(()=>{
                currentPage.value++ //新加了一页,意思是我想看到的下一页的页数
            })
        }
        
    }
    const isLastPage = computed(()=>{   //判定当前页是否是最后一页,Math.ceil函数它返回的是大于或等于函数参数，并且与之最接近的整数
        return Math.ceil(total.value / params.pageSize) < currentPage.value
    })
    return {
        loadMorePage,
        isLastPage,
        currentPage
    }
}

export default useLoadMore